<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>权限分配</title>
        <{$showjs}>
        <link rel="stylesheet" type="text/css" href="../css/main.css" />
        <style>
            .userlist{background-image:url(../img/shield.png)!important;}
            .useradd{background-image:url(../img/shield_add.png)!important;}
            .userkey{background-image:url(../img/group_key.png)!important;}
            .usercfg{background-image:url(../img/group_gear.png)!important;}
            .zdpz{background-image:url(../img/list_settings.gif)!important;}
            body { background-color: #D1DDEF; }
        </style>
        <script language="javascript">
            var isGroupChange = false,
                isComeFromUser = false;
            //所有会员
            var ds = new Ext.data.Store({
                autoLoad:true,
                //url:'/gl/right/get_users.php',
                proxy:new Ext.data.HttpProxy({
                    url:'/gl/right/get_users.php'
                }),
                reader: new Ext.data.JsonReader({
                        totalProperty: "total",
                        root:'result'
                    },[
                        {name:'id',type:'int'},
                        {name:'user'}
                    ]
                )
            });
            var colModel = new Ext.grid.ColumnModel([
                {id:'id',header:'UID',dataIndex:'id',sortable:true},
                {header:'用户名',dataIndex:'user',sortable:true}
            ]);
            //所有权限组
            var ds_group = new Ext.data.Store({
                autoLoad:true,
                //url:'/gl/right/get_users.php',
                proxy:new Ext.data.HttpProxy({
                    url:'/gl/right/get_users.php?t=group'
                }),
                reader: new Ext.data.JsonReader({
                        totalProperty: "total",
                        root:'result'
                    },[
                        {name:'id',type:'int'},
                        {name:'title'},
                        {name:'intro'}
                    ]
                )
            });
            var colModel_group = new Ext.grid.ColumnModel([
                new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
                {id:'id',header:'GID',dataIndex:'id',sortable:true},
                {header:'组名',dataIndex:'title',sortable:true},
                {header:'简介',dataIndex:'intro'}
            ]);
            //重置用户权限组
            var resetGroup = function(){
                //获取选中会员权限组
                var usm = Ext.getCmp('all-user-grid').getSelectionModel();
                var select_total = usm.getCount();
                if (select_total<1){
                    usm.clearSelections();
                    return;
                }
                var uid = usm.getSelected().get('id');
                Ext.Ajax.request({
                    url:'/gl/right/get_users.php?t=user',
                    method:'POST',
                    success:function(r){
                        var cfg = r.responseText.split(',');
                        isComeFromUser = true;  //来自会员点击
                        var cfg_range = Ext.getCmp('all-group-grid').getStore().queryBy(function(r){
                            //判断是否是拥有权限
                            return cfg.indexOf(r.get('id')+'') !== -1;
                        },this).getRange();
                        //选择拥有的权限组
                        Ext.getCmp('all-group-grid').getSelectionModel().selectRecords(cfg_range);
                        isComeFromUser = false;  //来自会员点击
                        isGroupChange = false;
                    },
                    failure:function(){
                        Ext.Msg.alert('提示','数据读取出错，请选择组权限"重置"操作');
                    },
                    params:{uid:uid}
                });
            };
            //保存用户权限组
            var saveGroup = function(){
                var usm = Ext.getCmp('all-user-grid').getSelectionModel();
                var gsm = Ext.getCmp('all-group-grid').getSelectionModel();
                var uid = usm.getSelected().get('id');
                var gid = gsm.getSelections();
                var gids = new Array();
                Ext.each(gid,function(a){
                    gids.push(a.get('id'));
                });
                //数组组成字符串
                var rcft = gids.join(',');
                Ext.Ajax.request({
                    url:'/gl/right/get_users.php?t=save',
                    method:'POST',
                    success:function(r){
                        if(r.responseText == 'ok'){
                            Ext.Msg.alert('提示','恭喜您，保存成功！');
                            isGroupChange = false;
                        }
                    },
                    failure:function(){
                        Ext.Msg.alert('提示','网络出错，请重新选择"保存"操作');
                    },
                    params:{
                        uid:uid,
                        gids:rcft
                    }
                });
            };
            
            
            //容器
            Ext.onReady(function(){
                var bd = Ext.getBody();
                
                var group = new Ext.Panel({
                    id:'group',
                    fram:true,
                    labelAlign:'left',
                    layout:'column',
                    autoWidth:true,
                    height:350,
                    items:[{
                        title:'系统用户',
                        columnWidth:0.26,
                        layout:'fit',
                        items:[{
                            id:'all-user-grid',
                            xtype:'grid',
                            autoScroll:true,
                            //autoHeight:true,
                            height:400,
                            ds:ds,
                            cm:colModel,
                            sm: new Ext.grid.RowSelectionModel({
                                singleSelect:true,
                                listeners:{
                                    rowselect: function(sm, row, rec) {
                                        resetGroup();
                                    },
                                    beforerowselect: function(){
                                        
                                        if (isGroupChange){
                                             if(confirm('您修改的值尚未保存，是否继续操作?')){
                                                 isGroupChange = false;
                                                    return true;
                                             }
                                            return false;
                                        }
                                    }
                                }
                            })
                        }]
                    },{
                        title:'系统权限组',
                        columnWidth:0.7,
                        style:'margin-left:30px',
                        tools:[{
                            id:'save',
                            on:{
                                click:saveGroup
                            }
                        }],
                        items:[{
                            id:'all-group-grid',
                            xtype:'grid',
                            //autoHeight:true,
                            autoScroll:true,
                            height:350,
                            autoExpandMax :true,
                            ds:ds_group,
                            cm:colModel_group,
                            sm:new Ext.grid.CheckboxSelectionModel({
                                singleSelect:false,
                                handleMouseDown: Ext.emptyFn,
                                listeners:{
                                    selectionchange:function(){
                                        if (!isComeFromUser){
                                            isGroupChange = true;
                                            isComeFromUser = false;
                                        }
                                    },
                                    beforerowselect:function(){
                                        var n = Ext.getCmp('all-user-grid').getSelectionModel().getCount();
                                        if(n<1){
                                            Ext.Msg.alert('提示','请先在左侧选择用户才能分配权限组!');
                                            return false;
                                        }
                                    }
                                }
                            }),
                            listeners:{
                                rowdblclick:function(obj,rowIndex){
                                    var csm = obj.getSelectionModel();
                                    csm.selectRow(rowIndex,true);
                                }
                            },
                            tbar:[
                                //new Ext.Toolbar.TextItem('修改后请保存'),
                                {
                                    pressed:false,
                                    text:'重置',
                                    listeners:{
                                        click:function(){
                                            resetGroup();
                                        }
                                    }
                                },
                                {
                                    pressed:false,
                                    text:'保存',
                                    listeners:{
                                        click:saveGroup
                                    }
                                }
                            ]
                            //sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
                        }]
                    }],
                    renderTo:Ext.getBody()
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>